﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PTiRouZ.Net.SilverLight.Api.HelloTipi;
using System.Collections.ObjectModel;
using PTiRouZ.Net.SilverLight.Api;
using Seesmic.Sdp.Extensibility;
using System.ComponentModel;
using Seesmic.Sdp.Utils;

using System.Linq;

namespace Seesmic.HelloTipi
{
    public class FamilyPhotosControlModel : PhotosControlModel<album_photos>
    {
        private string familySite;

        private int AlbumDefault_id;       

        protected ObservableCollection<site_albums> _albums;
        public ObservableCollection<site_albums> Albums
        {
            get { return _albums; }
            set { _albums = value; OnPropertyChanged("Albums"); }
        }

        public FamilyPhotosControlModel(HelloTipiAccount Account, string _familySite)
            : base(Account)
        {
            this.family = true;
            familySite = _familySite;

            this.api.SiteAlbumsCompleted += new SiteAlbumsEventHandler(api_SiteAlbumsCompleted);
            this.api.site_albums(familySite);

            //HelloTipiPlugin.LogInfo("api ???? " + this.api);

            while (this.api == null) ;
            
            //HelloTipiPlugin.LogInfo("Appel de l'api AlbumPhoto avec " + this.api);

            //this.api.AlbumPhotoCompleted += new AlbumPhotosEventHandler(api_AlbumPhotoCompleted);
            //this.api.AlbumPhoto(familySite);
        }

        private int _selectedAlbumIndex;
        public int SelectedAlbumIndex
        {
            get { return _selectedAlbumIndex; }
            set { _selectedAlbumIndex = value; OnPropertyChanged("SelectedAlbumIndex"); }
        }
        
        private site_albums _selectedAlbum;
        public site_albums SelectedAlbum
        {
            get { return _selectedAlbum; }
            set
            {                
                this.api.AlbumPhotoCompleted += delegate(object sender, AlbumPhotosEventArgs e)
                {
                    PhotosCollection = e.result as ObservableCollection<album_photos>;
                };
                this.api.AlbumPhoto(familySite, value.id);
                
                _selectedAlbum = value;
            }
        }

        void api_SiteAlbumsCompleted(object sender, SiteAlbumsEventArgs e)
        {
            Albums = e.result as ObservableCollection<site_albums>;

            site_albums sa = (from site_albums al in Albums
                              where al.name == "unsorted"
                              select al).FirstOrDefault();

            if (sa != null)
               AlbumDefault_id = sa.id;
            
            Albums.Remove(sa);

            Albums.Insert(0, new site_albums() { id = 0, name = "Photos non triées" });

            SelectedAlbum = (from site_albums al in Albums
                                 select al).FirstOrDefault();

        }

        void api_AlbumPhotoCompleted(object sender, AlbumPhotosEventArgs e)
        {
            this.api.AlbumPhotoCompleted -= new AlbumPhotosEventHandler(api_AlbumPhotoCompleted);

            PhotosCollection = e.result as ObservableCollection<album_photos>;

            //HelloTipiPlugin.LogInfo("Appel de l'api site_albums avec " + this.api);
            this.api.SiteAlbumsCompleted += new SiteAlbumsEventHandler(api_SiteAlbumsCompleted);
            this.api.site_albums(familySite);
        }

        public override void OnUploadPhotoExecuted()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = false;
            dlg.Filter = "All files (*.*)|*.*|JPG Images (*.jpg)|*.jpg|PNG Images (*.png)|*.png";

            bool? retval = dlg.ShowDialog();
            if (retval != null && retval == true)
            {
                this.api.UploadCompleted += new UploadEventHandler(UploadCompleted);
                this.api.upload(this.familySite, dlg.File.OpenRead(), dlg.File.Name);
            }
        }

        void UploadCompleted(object sender, UploadEventArgs e)
        {
            this.api.UploadCompleted -= UploadCompleted;
            if(!String.IsNullOrWhiteSpace(Comment)) {
                this.api.AddCommentCompleted += new AddCommentEventHandler(AddCommentAfterUploadCompleted);
                this.api.add_comment(e.result.photo_id, Comment, e.result.album_id);
            } else
                SynchronizationHelper.Post(() => HelloTipiPlugin.ShowDialogNotification("Envoie de la photo effectué", "la photo {0} à bien été envoyé", DialogNotificationButtons.OK, e.filename)); 
        }

        public override void OnCommentPhotoExecuted()
        {
            //HelloTipiPlugin.LogInfo("Appel de l'api add_comment avec " + this.api);
            this.api.AddCommentCompleted += new AddCommentEventHandler(AddCommentCompleted);

            //if (SelectedAlbum.default_album == 0)
            if(SelectedAlbum.id > 0)
                this.api.add_comment(PhotoSelected.id, Comment, SelectedAlbum.id);
            else
                this.api.add_comment(PhotoSelected.id, Comment, AlbumDefault_id);
        }

        void AddCommentCompleted(object sender, AddCommentEventArgs e)
        {
            
            this.api.AddCommentCompleted -= AddCommentCompleted;
            
            SynchronizationHelper.Post(() => HelloTipiPlugin.ShowDialogNotification("Commentaire envoyé", "{0}", DialogNotificationButtons.OK, Comment));

            if (e.result.status == "ok")
                SynchronizationHelper.Post(() => Comment = string.Empty); 
           
        }

        void AddCommentAfterUploadCompleted(object sender, AddCommentEventArgs e)
        {
            this.api.AddCommentCompleted -= AddCommentAfterUploadCompleted;

            SynchronizationHelper.Post(() => HelloTipiPlugin.ShowDialogNotification("Envoie de la photo effectué", "la photo à bien été envoyé avec le commentaire :\r\n{0}", DialogNotificationButtons.OK, Comment));           

            if (e.result.status == "ok")
                SynchronizationHelper.Post(() => Comment = string.Empty); 

        }

        protected override bool CanCommentPhoto()
        {
            return true;
            
            /*if (SelectedAlbum != null)
                return SelectedAlbum.id != 0;
            else
                return false;*/
        }

        protected override bool CanUploadPhoto()
        {
            return true;
        }

    }
}
